<?php
$loader->requireOnce('includes/PatientQueue/AbsQueue.class.php');
$loader->requireOnce('ordo/Department.class.php');

class InProgressQueue extends AbsQueue {

	private $queueType;	
	private $queueId;
	
	const ENCOUNTER_VIEW = "encounter";
	const CASHIER_VIEW = "cashier";
	const DOCTOR_VIEW = "doctor";
	
	private $viewType;
	
	function __construct($type) {
		$this->queueType = $type;
		$this->viewType = self::ENCOUNTER_VIEW;
		
		$queue = Celini::newORDO('Queue');
		$id = $queue->getIdFromType($this->queueType);
		if ($id === false) {
			throw new PatientQueueException("Queue $type is not availabe in database");
		}
		
		$this->queueId = $id;
	}
	
	public function getType() {
		return $this->queueType;
	}
	
	/**
	 * Implement abstract function
	 *
	 */
	public function getQueueId() {
		return $this->queueId;
	}
		
	public function setViewType($viewType) {
		$this->viewType = $viewType;
	}
	
	/**
	 * Implement abstract function
	 *
	 * @param int $roomId
	 */
	public function renderQueueAjax($roomId,$departmentId) {
		switch ($this->viewType) {
			case self::ENCOUNTER_VIEW:
				return $this->renderEncounterQueue($roomId,$departmentId);
			case self::CASHIER_VIEW:
				return $this->renderCashierQueue($roomId,$departmentId);
			case self::DOCTOR_VIEW:
				return $this->renderDoctorQueue($roomId,$departmentId);
			default:
				return "";
		}
		
	}
	
	/**
	 * Implement abstract function
	 *
	 * @param int $roomId
	 * @param int $itemPerPage
	 * @param int $page
	 */
	public function renderQuickQueueAjax($roomId, $itemPerPage, $page, $holderId = 0) {
		return $this->renderQuickQueue($roomId, $itemPerPage, $page, $holderId);
	}
	
	private function renderEncounterQueue($roomId,$departmentId) {
		$queueItem = Celini::newORDO('QueueItem');
		$res = $queueItem->getAll($this->queueId, $roomId, $departmentId);
		$body = "";
		$count = 1;
				
		$ticketItem = Celini::newORDO('TicketItem');		
		
		foreach ($res as $row) {					
			$patientId = $row['patient_id'];
			
			$temp = $ticketItem->serviceNameAndPaymentType($row['ticket_item_id']);
			$serviceName = $temp['name'];										
			
			$aRow = "<tr id=\"" . $row['ticket_item_id'] . "\">";
			$aRow .= "<td>$count</td>";
			$aRow .= "<td><a href=\"" .Celini::link('ViewMedicalRecord', 'Patient') . "patientId=$patientId&display=service" ."\">" . $row['patient_name'] . "</a></td>";
			$aRow .= "<td>" . $row['patient_code'] . "</td>";			
			$aRow .= "<td>" . $row['room_name'] . "</td>";
			$aRow .= "<td>" . $serviceName . "</td>";
			$aRow .= "<td>" . L10nHelper::renderDateTime($row['entry_time'], 'short', 'datetime') . "</td>";
			$aRow .= "<td>" . L10nHelper::_('%s mins',$row['waiting_time']). "</td>";		

			$doctor = Celini::newORDO('Person', $row['holder_id']);
			$aRow .= "<td>" . $doctor->get('name') . "</td>";
			
			$aRow .= "</tr>";
			
			$count++;
			$body .= $aRow;														
		}
		$total = '<table cellspacing="0" cellpadding="0" style="width: 100%;">';
		$total .= '<tr class="last"><td>'.L10nHelper::_("Total: <span class='impression'>%s<span>",--$count).'</td></tr>';
		$total .= '</table>';				
		if ($body == '') {
			$body = '<tr class="last"><td colspan="8">' . L10nHelper::_('There is no result') . '</td></tr>';
		}
		return array('body' => $body, 'total_table' => $total);
	}
	
	private function renderCashierQueue($roomId,$departmentId) {
		$queueItem = Celini::newORDO('QueueItem');
		$res = $queueItem->getAll($this->queueId, $roomId, $departmentId);
		$body = "";
		$count = 1;
				
		$ticketItem = Celini::newORDO('TicketItem');
				
		foreach ($res as $row) {					
			$patientId = $row['patient_id'];
			
			$temp = $ticketItem->serviceNameAndPaymentType($row['ticket_item_id']);
			$serviceName = $temp['name'];			
			$paymentType = $temp['payment_type'];
			if ($paymentType == Service::AFTER) {
				$paymentType = L10nHelper::_("All");
			} else if ($paymentType == Service::BEFORE) {
				$paymentType = L10nHelper::_("Prepaid");
			}	
			
			$aRow = "<tr id=\"" . $row['ticket_item_id'] . "\">";
			$aRow .= "<td>$count</td>";
			$aRow .= "<td><a href=\"" . Celini::link('ViewInprogressItem', 'Cashier') . "ticketItemId=" . $row['ticket_item_id']  ."\">" . $row['patient_name'] . "</a></td>";
			$aRow .= "<td>" . $row['patient_code'] . "</td>";			
			
			$aRow .= "<td>" . $serviceName . "</td>";	
			$aRow .= "<td>" . $paymentType . "</td>";
			$aRow .= "<td>" . L10nHelper::_('%s mins',$row['waiting_time']) . "</td>";
			
			$cashier = Celini::newORDO('Person', $row['holder_id']);
			$aRow .= "<td>" . $cashier->get('name') . "</td>";
			$aRow .= "</tr>";
			
			$count++;
			$body .= $aRow;														
		}
		$total = '<table cellspacing="0" cellpadding="0" style="width: 100%;">';
		$total .= '<tr class="last"><td>'.L10nHelper::_("Total: <span class='impression'>%s<span>",--$count).'</td></tr>';
		$total .= '</table>';				
		if ($body == '') {
			$body = '<tr class="last"><td colspan="7">' . L10nHelper::_('There is no result') . '</td></tr>';
		}
		return array('body' => $body, 'total_table' => $total);
	}
	
	private function renderDoctorQueue($roomId,$departmentId) {
		$queueItem = Celini::newORDO('QueueItem');
		$res = $queueItem->getAll($this->queueId, $roomId, $departmentId);
		$body = "";
		$count = 1;
				
		$ticketItem = Celini::newORDO('TicketItem');		
		
		foreach ($res as $row) {					
			$patientId = $row['patient_id'];
			
			$temp = $ticketItem->serviceNameAndPaymentType($row['ticket_item_id']);
			$serviceName = $temp['name'];										
			
			$aRow = "<tr id=\"" . $row['ticket_item_id'] . "\"  class='contextMenu'>";
			$aRow .= "<td>$count</td>";
			$aRow .= "<td><a href=\"" .Celini::link('viewProgressEncounter', 'Patient') . "patientId=$patientId&display=encounter&ticketItemId=" . $row['ticket_item_id'] ."\">" . $row['patient_name'] . "</a></td>";
			$aRow .= "<td>" . $row['patient_code'] . "</td>";			
			
			$aRow .= "<td>" . $serviceName . "</td>";
			$aRow .= "<td>" . L10nHelper::renderDateTime($row['entry_time'], 'short', 'datetime') . "</td>";
			$aRow .= "<td>" . L10nHelper::_('%s mins',$row['waiting_time']) . "</td>";		

			$doctor = Celini::newORDO('Person', $row['holder_id']);
			$aRow .= "<td>" . $doctor->get('name') . "</td>";
			$aRow .= "</tr>";
			
			$count++;
			$body .= $aRow;														
		}
		$total = '<table cellspacing="0" cellpadding="0" style="width: 100%;">';
		$total .= '<tr class="last"><td>'.L10nHelper::_("Total: <span class='impression'>%s<span>",--$count).'</td></tr>';
		$total .= '</table>';				
		if ($body == '') {
			$body = '<tr class="last"><td colspan="7">' . L10nHelper::_('There is no result') . '</td></tr>';
		}
		return array('body' => $body, 'total_table' => $total);
	}
	
	private function renderQuickQueue($roomId, $itemPerPage, $page, $holderId = 0) {
		$body = '';
		$queueItem = Celini::newORDO('QueueItem');
		$totalResults = $queueItem->countItems($this->queueId, $roomId, $holderId);
		if ($totalResults > 0) {
			$res = $queueItem->getPartial($this->queueId, $roomId, $itemPerPage, $page, $holderId);
			foreach ($res as $row) {
				$body .= "<div class='module_item' title='". L10nHelper::_("Select patient %s", $row['patient_name']). "' id='{$row['ticket_item_id']}' onclick='pickInprogressItem({$row['ticket_item_id']}, {$row['patient_id']})'>";
				$body .= "<div class='name'>{$row['patient_name']}</div>";
				$body .= "<div style='text-align: right'>{$row['patient_code']}</div></div>";
			}
		} else {
			$body .= "<div class='module_item' style='line-height: 37px;'>Empty result</div>";
		}
		return array('body' => $body, 'totalPage' => ceil($totalResults/$itemPerPage));
	}
}

?>